package edu.cuny.baruch.cis9590.library.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.cuny.baruch.cis9590.library.database.BookDAO;
import edu.cuny.baruch.cis9590.library.entities.Book;

final class AddBookPanel extends LibraryPanel {

    private final JTextField authorField;
    private final JTextField callNumberField;
    private final JTextField titleField;
	private final BookDAO bookDAO;
    
    public AddBookPanel() {
        this.bookDAO = new BookDAO();
        
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        setLayout(gridBagLayout);
        
        JLabel titleLabel = new JLabel("Title");
        GridBagConstraints gbc_titleLabel = new GridBagConstraints();
        gbc_titleLabel.insets = new Insets(0, 0, 5, 5);
        gbc_titleLabel.anchor = GridBagConstraints.EAST;
        gbc_titleLabel.gridx = 1;
        gbc_titleLabel.gridy = 1;
        add(titleLabel, gbc_titleLabel);
        
        titleField = new JTextField();
        GridBagConstraints gbc_titleField = new GridBagConstraints();
        gbc_titleField.insets = new Insets(0, 0, 5, 5);
        gbc_titleField.fill = GridBagConstraints.HORIZONTAL;
        gbc_titleField.gridx = 2;
        gbc_titleField.gridy = 1;
        add(titleField, gbc_titleField);
        titleField.setColumns(25);
        
        JLabel authorLabel = new JLabel("Author");
        GridBagConstraints gbc_authorLabel = new GridBagConstraints();
        gbc_authorLabel.anchor = GridBagConstraints.EAST;
        gbc_authorLabel.insets = new Insets(0, 0, 5, 5);
        gbc_authorLabel.gridx = 1;
        gbc_authorLabel.gridy = 2;
        add(authorLabel, gbc_authorLabel);
        
        authorField = new JTextField();
        GridBagConstraints gbc_authorField = new GridBagConstraints();
        gbc_authorField.insets = new Insets(0, 0, 5, 5);
        gbc_authorField.fill = GridBagConstraints.HORIZONTAL;
        gbc_authorField.gridx = 2;
        gbc_authorField.gridy = 2;
        add(authorField, gbc_authorField);
        authorField.setColumns(25);
        
        JLabel callNumberLabel = new JLabel("Call Number");
        GridBagConstraints gbc_callNumberLabel = new GridBagConstraints();
        gbc_callNumberLabel.anchor = GridBagConstraints.EAST;
        gbc_callNumberLabel.insets = new Insets(0, 0, 5, 5);
        gbc_callNumberLabel.gridx = 1;
        gbc_callNumberLabel.gridy = 3;
        add(callNumberLabel, gbc_callNumberLabel);
        
        callNumberField = new JTextField();
        GridBagConstraints gbc_callNumberField = new GridBagConstraints();
        gbc_callNumberField.insets = new Insets(0, 0, 5, 5);
        gbc_callNumberField.fill = GridBagConstraints.HORIZONTAL;
        gbc_callNumberField.gridx = 2;
        gbc_callNumberField.gridy = 3;
        add(callNumberField, gbc_callNumberField);
        callNumberField.setColumns(25);
        
        JButton addBookButton = new JButton("Add Book");
        GridBagConstraints gbc_addBookButton = new GridBagConstraints();
        gbc_addBookButton.insets = new Insets(0, 0, 5, 5);
        gbc_addBookButton.gridx = 2;
        gbc_addBookButton.gridy = 4;
        add(addBookButton, gbc_addBookButton);
        addBookButton.addActionListener(new AddBookActionListener());
    }
    
    private final class AddBookActionListener implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            String author = authorField.getText().trim();
            String title = titleField.getText().trim();
            String callNumber = callNumberField.getText().trim();
            
            if ("".equals(author) || "".equals(title) || "".equals(callNumber)) {
                JOptionPane.showMessageDialog(AddBookPanel.this, "Please complete all fields.");
                return;
            }
            
            bookDAO.insert(new Book(author, title, 'A', callNumber));
            clear();
        }
    }
    
    @Override
    public void clear() {
        authorField.setText("");
        titleField.setText("");
        callNumberField.setText("");
    }
}
